module extraepd.dexp;
import expd.ibase;
import expd.itree;
import expd.itoken;
import expd.istack;
import expd.stack;
import expd.node;
import extraepd.dexpd.all;
import expd.comp;
version(dbg3)
{
    import expd.meta;
    mixin (DebugTrick());
}
class DExpParser : Streamable! ( ITree!Token ) {
    alias node!Token Node;
    DElem top;
    string elem;
    size_t len;
    public this() {

    }
    //iterated version
    ///Push to do semantic analysis on a tree.
    void push ( ITree!Token x ) {
      doNode ( x.getBase(), top );
    }

    void flush() {}

    ///Called by push to walk tree RECURSIVELY
    void doNode ( Node toProc, ref DElem ptr ) {
      ptr.values.length = toProc.nb;
      Token tok = toProc.sym;
      string info;
      if ( toProc.nb <= 1 ) {

        if(toProc.nb == 0) info = toProc.sym.token;
        else info = toProc.down.sym.token;

          switch ( tok.type ) {
              case TOKEN.INT:
                ptr.value = DVal ( To! ( long ) ( info ) );
                break;
              case TOKEN.FLOAT:
                ptr.value = DVal ( To! ( float ) ( info ) );
                break;
              case TOKEN.O_PAR: //Must test
                DElem var;

                if ( toProc.down.nb > 0 ) {
                    doNode ( *toProc.down, var );
                    ptr = var;
                  }
                else {
                    ptr.value = DVal ( null );
                  }

                break;
              case TOKEN.STRLIT:
                ptr.value = DVal ( info );
                break;
              case TOKEN.NULL:
                ptr.value = DVal ( null );
              case TOKEN.ID:
                if( info )
                ptr.value = DVal ( info );
              default:

            }
        }
      else if ( toProc.nb > 1 ) {
          ptr.isList = true;
          Node *cnode = toProc.down;
          foreach ( ref a; ptr.values ) {
            assert(cnode !is null,"mismatched arrays on info");
            doNode ( *cnode, a );
            cnode = cnode.next;
            if(cnode.down.nb > 1){
                if(cnode.down.sym.token != "")ptr.named[cnode.down.sym.token]=a;
            }
          }
        }
      else {
          ptr.value = DVal ( null );
        }
    }
    DElem getTop(){
        return top;
    }

}
